<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">

    <!-- imports formatting constants and layout functions common to multiple documents-->    
    <xsl:include href="../include/reportConstants.xsl"/>
    <xsl:include href="../include/reportAttrSets.xsl"/>
    <xsl:include href="../include/reportUtils.xsl"/>

    <xsl:attribute-set name="this-report-header">
        <xsl:attribute name="font-family">serif</xsl:attribute> 
        <xsl:attribute name="font-weight">bold</xsl:attribute> 
        <xsl:attribute name="white-space-collapse">true</xsl:attribute>
    </xsl:attribute-set> 

    <xsl:decimal-format NaN=""/>

    <xsl:template match="ns:CityDetailsReport" xmlns:ns="http://flightreports.googlecode.com/citydetails">

        <!-- fo:root is the top element of any printed item (document or book, etc.)-->
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="landscapeMaster" 
                    page-width="8.5in" 
                    page-height="11.0in"
                    margin-top="0.6in" 
                    margin-bottom="0.6in"
                    margin-left="0.6in" 
                    margin-right="0.6in">
                    <fo:region-body margin-top="0.8in" margin-bottom="0.0in"/>
                    <fo:region-before extent="1.4in"/>
                    <fo:region-after extent="0.0in"/>
                </fo:simple-page-master>
            </fo:layout-master-set>

            <fo:page-sequence master-reference="landscapeMaster">

                <!-- Header -->
                <fo:static-content flow-name="xsl-region-before">
                   <fo:table xsl:use-attributes-sets="this-report-header" table-layout="fixed" font-size="12pt" line-height="14pt">
                       <!-- landscape 108 spaces; portrait 80 -->
		       <fo:table-column column-width="11pt * 0.60 * (20 + 0)"/>
                   	  <fo:table-column column-width="11pt * 0.60 * (40 + 0)"/>
                   	  <fo:table-column column-width="11pt * 0.60 * (20 + 0)"/>                   	
                        <fo:table-body>          
                          <fo:table-row>
                            <fo:table-cell>
                              <fo:block text-align="start">Run Time:</fo:block>
                            </fo:table-cell>
                             
                            <fo:table-cell>
                              <fo:block text-align="center"><xsl:call-template name="system-report-title"/></fo:block>
                            </fo:table-cell>
                             
                            <fo:table-cell>
                              <fo:block text-align="end">Page: <fo:page-number/> of <fo:page-number-citation ref-id="endofdoc"/></fo:block>
                            </fo:table-cell>
                         </fo:table-row>

                         <fo:table-row>
                            <fo:table-cell>
                              <fo:block text-align="start"><xsl:value-of select='./runDate'/></fo:block>
                            </fo:table-cell>
                          <fo:table-cell>
                            <fo:block text-align="center">City Details Report for <xsl:value-of select="./cityName"/></fo:block>
                          </fo:table-cell>
                        </fo:table-row>

                          <xsl:call-template name="create-blank-row"/>
                     </fo:table-body>
                   </fo:table>
                                  
                  </fo:static-content>
		  
                <!--Body -->
                <fo:flow flow-name="xsl-region-body">

                    <fo:block>City:  <xsl:value-of select="./cityName"/></fo:block>
                    <fo:block>Country:  <xsl:value-of select="./country"/> (<xsl:value-of select="./countryISOCode"/>)</fo:block>
                    <fo:block>Airport Code:  <xsl:value-of select="./airport"/></fo:block>
                    <fo:block>Language:  <xsl:value-of select="./language"/></fo:block>

                    <fo:block space-before.optimum="2em">
                    <fo:block font-weight="bold" font-size="12pt" line-height="14pt">Departures:</fo:block>
                    <xsl:apply-templates select="./departures"/>
                    <fo:block font-weight="bold" font-size="12pt" line-height="14pt" space-before.optimum="2em">Arrivals:</fo:block>
                    <xsl:apply-templates select="./arrivals"/>
                    </fo:block>
                    
                    <fo:block id="endofdoc"/>
           </fo:flow>                          
        </fo:page-sequence>
     </fo:root>
  </xsl:template>
  
  <xsl:template match="arrivals | departures">
      <fo:table table-layout="fixed" space-before.optimum="2em">
          <fo:table-column column-width="10pt * 0.65 * (6 + 1)"/>
          <fo:table-column column-width="10pt * 0.65 * (18 + 1)"/>                          
          <fo:table-column column-width="10pt * 0.65 * (5 + 1)"/>
          <fo:table-column column-width="10pt * 0.65 * (5 + 1)"/>
          <fo:table-column column-width="10pt * 0.65 * (5 + 1)"/>
          <fo:table-column column-width="10pt * 0.65 * (9 + 1)"/>
          <fo:table-column column-width="10pt * 0.65 * (7 + 0)"/>
               <fo:table-header xsl:use-attribute-sets="table-char-cell">
                   <fo:table-row font-weight="bold" white-space-collapse="false">
                        <fo:table-cell><fo:block text-align="center">Flight</fo:block></fo:table-cell>
                        <xsl:choose>
                            <xsl:when test='./arrival'>
                                <fo:table-cell padding-top="2pt">
                                    <fo:block text-align="center">Origin</fo:block>
                                </fo:table-cell>
                            </xsl:when>
                            <xsl:otherwise>
                                <fo:table-cell padding-top="2pt">
                                    <fo:block text-align="center">Destination</fo:block>
                                </fo:table-cell>
                            </xsl:otherwise>
                        </xsl:choose>
                        <fo:table-cell><fo:block text-align="center">Depart</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block text-align="center">Arrive</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block text-align="center"></fo:block></fo:table-cell>
                        <fo:table-cell><fo:block text-align="center">Number Of</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block text-align="center">Aircraft</fo:block></fo:table-cell>
                    </fo:table-row>

                    <fo:table-row font-weight="bold" white-space-collapse="false">
                        <fo:table-cell><fo:block border-bottom="1pt solid #000000" text-align="center">Number</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block border-bottom="1pt solid #000000" text-align="center">City</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block border-bottom="1pt solid #000000" text-align="center">Time</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block border-bottom="1pt solid #000000" text-align="center">Time</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block border-bottom="1pt solid #000000" text-align="center">Miles</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block border-bottom="1pt solid #000000" text-align="center">Segments</fo:block></fo:table-cell>
                        <fo:table-cell><fo:block border-bottom="1pt solid #000000" text-align="center">Type</fo:block></fo:table-cell>
                    </fo:table-row>
               </fo:table-header>

             <fo:table-body>
                   <xsl:choose>
                        <xsl:when test="count(*) = 0">
                            <xsl:call-template name="create-blank-row"/>
                            <fo:table-row>
                                <fo:table-cell number-columns-spanned="2" padding-top="7pt">
                                    <fo:block>No Records Found</fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </xsl:when>
                       <xsl:otherwise>
                            <!-- all children, either arrival or departure -->
                            <xsl:apply-templates select="*"/> 
                       </xsl:otherwise>
                   </xsl:choose>
            </fo:table-body>
         </fo:table>
         
        <fo:table table-layout="fixed" space-before.optimum="1em">
                <fo:table-column column-width="10pt * 0.60 * (18 + 1)"/>
                <fo:table-column column-width="10pt * 0.60 * (10 + 0)"/>
                  <fo:table-body>
                     <fo:table-row keep-with-next="always">
                       <fo:table-cell><fo:block font-weight="bold">Total Flights: <xsl:value-of select='count(*)'/></fo:block></fo:table-cell>
                     </fo:table-row>                                                                                        
               </fo:table-body>                                    
         </fo:table>
  </xsl:template>
  
  <xsl:template match="arrival | departure">
        <fo:table-row xsl:use-attribute-sets="table-char-cell">
            <fo:table-cell padding-top="2pt">
                <fo:block><xsl:value-of select='./flightNumber'/></fo:block>
            </fo:table-cell>
            <xsl:choose>
                <xsl:when test='../arrival'>
                    <fo:table-cell padding-top="2pt">
                        <fo:block><xsl:value-of select='./origin'/></fo:block>
                    </fo:table-cell>
                </xsl:when>
                <xsl:otherwise>
                    <fo:table-cell padding-top="2pt">
                        <fo:block><xsl:value-of select='./destination'/></fo:block>
                    </fo:table-cell>
                </xsl:otherwise>
            </xsl:choose>
            <fo:table-cell padding-top="2pt" padding-left="9pt">
                <fo:block text-align="start"><xsl:value-of select='./departTime'/></fo:block>
            </fo:table-cell>
            <fo:table-cell padding-top="2pt" padding-left="9pt">
                <fo:block text-align="start"><xsl:value-of select='./arriveTime'/></fo:block>
            </fo:table-cell>
            <fo:table-cell padding-top="2pt" padding-right="8pt">
                <fo:block text-align="end"><xsl:value-of select='./miles'/></fo:block>
            </fo:table-cell>
            <fo:table-cell padding-top="2pt">
                <fo:block text-align="center"><xsl:value-of select='./numberOfSegments'/></fo:block>
            </fo:table-cell>
            <fo:table-cell padding-top="2pt" padding-left="10pt">
                <fo:block><xsl:value-of select='./aircraftType'/></fo:block>
            </fo:table-cell>
        </fo:table-row>
    </xsl:template>

</xsl:stylesheet>